﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using OpenTK;
using OpenTK.Graphics.OpenGL;

using Core;
using Core.Graphics;
using Core.Helpers;
using Core.Objects;
using Core.Physics;

namespace Terrain
{
    public partial class TerrainWindow : Form
    {
        private Engine engine;
        private Task genMap;
        private Timer mainTimer;
        private KeyHandler keyHandler;

        public TerrainWindow()
        {
            genMap = new Task(
                () =>
                {
                    GraphEngine graph = new GraphEngine(RenderFrame);
                    engine = new SimplePhysEngine(graph); //new Engine(graph);
                });
            genMap.ContinueWith((task) =>
            {
                var heightMap = engine.Map;
                //WriteToFile(heightMap);
                var context = WindowsFormsSynchronizationContext.Current;
                if (context != null)
                {
                    context.Send(new System.Threading.SendOrPostCallback(
                        (state) => { this.Text = state as string; }), "written");
                }
            });

            genMap.Start();
            mainTimer = new Timer()
            {
                Interval = 1000 / Constants.PREFERRED_FPS
            };
            mainTimer.Tick += gameTick;
            InitializeComponent();
        }

      /*  private void WriteToFile(HeightMap heightMap)
        {
            using (FileStream fstream = new FileStream("D:\\terrain.txt", FileMode.Create))
            {
                using (StreamWriter wr = new StreamWriter(fstream))
                {
                    for (int i = 0; i < 128; i++)
                    {
                        for (int j = 0; j < 128; j++)
                        {
                            wr.Write("{0:F2} ", heightMap[i, j]);
                        }
                        wr.WriteLine();
                    }
                }
            }
        }*/

        private void TerrainWindow_Load(object sender, EventArgs e)
        {
            //Application.Idle += Application_Idle;

            // engine = new Engine(128); 
            AbstractVehicle vehicle = new Helicopter();
            genMap.Wait();
            SetupViewport(engine.GraphicsEngine);
            engine.Vehicle = vehicle;
            keyHandler = new KeyHandler();
            keyHandler.OnKeyPress += engine.ProcessKey;
            engine.KeyHandler = keyHandler;
            mainTimer.Start();
        }

        private void gameTick(object sender, EventArgs e)
        {
            engine.Tick();
            this.Text = String.Format("x={0:F4}       z={1:F4}    angle={2:F4}   eyex={3:F4}   eyez={4:F4}", 
                engine.Vehicle.X, 
                engine.Vehicle.Z,  
                engine.Vehicle.AngleHorizontal
                ,engine.Vehicle.EyeX
                ,engine.Vehicle.EyeZ
                );
        }

        void Application_Idle(object sender, EventArgs e)
        {

        }

        private void RenderFrame()
        {
            viewBox.SwapBuffers();
            viewBox.Refresh();
        }

        private void SetupViewport(GraphEngine graph)
        {
            int width = viewBox.Width;
            int height = viewBox.Height;
            graph.InitGraphics(width, height);
        }
    }
}
